<template>
    <el-container v-loading.fullscreen.lock="loading">
        <el-header>
            您的订单
        </el-header>
        <el-main>
            <el-table :data="tableData">
                <el-table-column prop="id" label="订单号" />
                <el-table-column prop="description" label="描述" />
                <el-table-column prop="amount" label="金额" />
            </el-table>
        </el-main>
        <el-footer>
            <el-button type="danger" @click="pay">支付宝支付</el-button>
            <el-button @click="cancel">取消</el-button>
        </el-footer>
    </el-container>
</template>

<script>
    import {confirm, reject} from "@/api/confirm";

    export default {
        name: "PayConfirm",
        props: [ 'id', 'description', 'amount', 'redirect' ],
        data() {
            return {
                loading: false,
            };
        },
        computed: {
            tableData() {
                return [{
                    id: this.id,
                    description: this.description,
                    amount: this.amount,
                }];
            }
        },
        methods: {
            pay() {
                this.loading = true;
                confirm(this.id).then(value => {
                    if (value.status === 204) {
                        if (this.redirect) {
                            window.location = this.redirect;
                        } else {
                            this.$message.error('缺少重定向地址');
                        }
                    } else {
                        this.$alert('服务器错误').catch(() => {});
                    }
                }).catch(reason => {
                    this.$alert('发生错误：' + JSON.stringify(reason))
                        .catch(() => {});
                }).finally(() => {
                    this.loading = false;
                });
            },
            cancel() {
                this.loading = true;
                reject(this.id).then(value => {
                    if (value.status === 204) {
                        if (this.redirect) {
                            window.location = this.redirect;
                        } else {
                            this.$message.error('缺少重定向地址');
                        }
                    } else {
                        this.$alert('服务器错误').catch(() => {});
                    }
                }).catch(reason => {
                    this.$alert('发生错误：' + JSON.stringify(reason))
                        .catch(() => {});
                }).finally(() => {
                    this.loading = false;
                });
            }
        }
    }
</script>

<style scoped>

</style>
